package com.example.demo.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.example.demo.common.utills.CommonResult;
import com.example.demo.entity.Token;
import com.example.demo.entity.User;
import com.example.demo.entity.UserToken;
import com.example.demo.entity.UserTokenDto;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserTokenMapper;
import com.example.demo.service.LoginService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@CrossOrigin(allowCredentials = "true")
public class LoginController {
    @Resource
    LoginService loginService;

    @Resource
    UserMapper userMapper;

    @Resource
    UserTokenMapper userTokenMapper;

    @Resource
    RedisTemplate redisTemplate;

    @RequestMapping(value = "/login")
    public CommonResult Login(String account,String psd){
        return loginService.login(account,psd);
    }

//    @RequestMapping("/scanLogin")
//    public void ScanLogin(HttpServletResponse response){
//        response.setHeader("Pragma","No-cache");
//        response.setHeader("Cache-Control","no-cache");
//        response.setDateHeader("Expires",0);
//        response.setContentType("image/jpeg");
//        try {
//
//        }catch ()
//    }

    @RequestMapping("/scanLogin")
    public void scanLogin(HttpServletResponse response){
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        try {
            //这里没啥操作 就是生成一个UUID插入 数据库的表里
            String uuid = IdUtil.simpleUUID();
            userTokenMapper.insertToken(new UserToken(uuid,0,"0"));
            response.setHeader("uuid", uuid);
            // 这里是开源工具类 hutool里的QrCodeUtil
            // 网址：http://hutool.mydoc.io/
            QrCodeUtil.generate("https://192.168.2.38:8081/#/qrConfirm?uuid="+uuid, 300, 300, "jpg",response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/status") //查询状态
    public CommonResult Status(String uuid){
        UserToken userToken = userTokenMapper.selectByUuid(uuid);
        System.out.println(userToken);
        if(userToken.getStatus().equals("2")){
            int uid = userToken.getUid();
            String token= (String) redisTemplate.opsForValue().get(String.valueOf(uid));
            UserTokenDto userTokenDto=new UserTokenDto();
            userTokenDto.setStatus("2");
            userTokenDto.setToken(token);
            userTokenDto.setUid(uid);
            return new CommonResult(200,null,userTokenDto);
        }
        return new CommonResult(200,null,userToken);
    }

    @RequestMapping("/updateStatus") //更新状态
    public CommonResult updateStatus(String uuid,int uid){
        userTokenMapper.updateTokenStatus("1",uuid,uid);
        return new CommonResult(200,null,null);
    }

    @RequestMapping("/verify")  //确认
    public CommonResult verifyUser(String uuid,int uid){
        userTokenMapper.updateTokenStatus("2",uuid,uid);
        User user = userMapper.selectById(uid);
        UserToken userToken = userTokenMapper.selectByUuid(uuid);
        if(user.getUid()==userToken.getUid()){
            return new CommonResult(200,null,user);
        }
        return new CommonResult(500,"失败",null);
    }
}
